package cn.ztydata.teachingsystem.heaven.service;

import cn.ztydata.teachingsystem.common.Page;
import cn.ztydata.teachingsystem.exception.ServiceException;
import cn.ztydata.teachingsystem.heaven.common.WebContext;
import cn.ztydata.teachingsystem.heaven.cons.Roles;
import cn.ztydata.teachingsystem.heaven.dao.DailyCommentDao;
import cn.ztydata.teachingsystem.heaven.dao.DailyDao;
import cn.ztydata.teachingsystem.heaven.dao.SpecialtyDao;
import cn.ztydata.teachingsystem.heaven.dao.redis.DailyComCountDao;
import cn.ztydata.teachingsystem.heaven.entity.Daily;
import cn.ztydata.teachingsystem.heaven.entity.DailyComment;
import cn.ztydata.teachingsystem.heaven.web.dto.SpecialtyGroup.DailyCommentDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * DailyCommentService
 *
 * @author lyj
 * @since 2015/5/7
 */
@Service
public class DailyCommentService extends BaseService {
    @Autowired
    private DailyCommentDao dailyCommentDao;
    @Autowired
    private WebContext webContext;
    @Autowired
    private DailyDao dailyDao;
    @Autowired
    private SpecialtyDao specialtyDao;
    @Autowired
    private DailyComCountDao dailyComCountDao;

    //<editor-fold desc="新增">
    /**
     * 新增教学日志评论
     *
     * @param dailyComment
     * @return void
     * @author lyj
     * @since 2015-5-7
     */
    public void addDailyComment(final DailyComment dailyComment) {
        //判断日志是否已经删除
        Daily daily = dailyDao.getDetailById(dailyComment.getDailyId());
        if (daily == null) {
            log.debug("获取日志明细失败daily == null");

            log.warn("获取日志明细失败！");
            throw new ServiceException("日志已经被删除，不能再评论！");
        }

        //判断日志回复是否已删除
        if (dailyComment.getCommentId() != 0) {
            if (dailyCommentDao.getDetail(dailyComment.getCommentId()) == null) {
                log.debug("获取日志回复明细失败dailyComment == null");

                log.warn("获取日志回复明细失败！");
                throw new ServiceException("回复已经被删除！");
            }
        }


        if (dailyCommentDao.addDailyComment(dailyComment) != 1) {
            log.debug("新增教学日志失败dailyCommentDao.addDailyComment(dailyComment) != 1");
            log.warn("新增教学日志失败!");

            throw new ServiceException("新增教学日志失败！");
        }
        //递增回复量
        System.out.print(dailyComCountDao.increment(dailyComment.getDailyId(), dailyComCountDao.DAILY_COMMENT_COUNT));
    }
    //</editor-fold>

    //<editor-fold desc="删除">
    /**
     * 删除评论（回复）
     *
     * @param commentId 回复ID
     * @param dailyId 日志Id
     * @return void
     * @author lyj
     * @since 2015-5-6
     */
    public void deleteById(final long commentId, final long dailyId) {

        if (webContext.getRole() != Roles.TEACHER && webContext.getRole() != Roles.SPEC_LEADER) {
            log.debug("登录用户没有权限使用该接口webContext.getRole() != Roles.TEACHER && webContext.getRole() != Roles.STUDENT");

            log.warn("登录用户没有权限使用该接口！");
            throw new ServiceException("权限错误！");
        }

        //判断日志回复是否已删除
        DailyComment dailyComment = dailyCommentDao.getDetail(commentId);
        if (dailyComment == null) {
            log.debug("获取日志回复明细失败dailyComment == null");

            log.warn("获取日志回复明细失败！");
            throw new ServiceException("回复已经被删除！");
        }

        //判断日志是否已经删除
        Daily daily = dailyDao.getDetailById(dailyId);
        if (daily == null) {
            log.debug("获取日志明细失败daily == null");

            log.warn("获取日志明细失败！");
            throw new ServiceException("日志已经被删除！");
        }

        //如果登录用户不是回复的创建人
        if (dailyComment.getCreatorId().longValue() != webContext.getUserId().longValue()) {
            log.debug("如果登录用户不是回复的创建人dailyComment.getCreatorId() != webContext.getUserId()");

            //判断登录老师是否是该专业的负责人
            if (specialtyDao.checkByLeader(daily.getGroupId(), webContext.getUserId()) != 1) {
                log.debug("判断登录老师是否是该专业的负责人specialtyDao.checkByLeader(daily.getGroupId(), webContext.getUserId()) != 1");

                log.warn("登录老师不是该专业的负责人");
                throw new ServiceException("权限错误！");
            }
        }

        if (dailyCommentDao.deleteById(commentId) != 1) {
            log.debug("删除教学日志失败dailyCommentDao.deleteById(commentId) != 1");

            log.warn("删除教学日志失败：id->{}", commentId);

            throw new ServiceException("删除教学日志失败！");
        }

        //递减回复量
        dailyComCountDao.subIncrement(dailyId, dailyComCountDao.DAILY_COMMENT_COUNT);
    }
    //</editor-fold>

    //<editor-fold desc="查询">
    /**
     * 根据日志ID获取回复列表
     *
     * @param dailyId 日志ID
     * @param pageNum 当前页码
     * @param pageSize 每页条数
     * @return Page<DailyComment>
     * @author lyj
     * @since 2015-5-6
     */
    public Page<DailyComment> getAllComment(final long dailyId, final int pageNum, final int pageSize) {
        log.debug("根据日志ID获取回复列表");

        return dailyCommentDao.getAllComment(dailyId, pageNum, pageSize);
    }
    //</editor-fold>

}
